var typeList = null;
var level2Menu;
var iconList;
var mBox;
var DEFAULT_IMG = 'images/default_m.png';
var recentDB = false;
var favDB = false;
$(function(){ 
	//初始化最近观看和收藏夹
	recentDB = new DB({name:'recent_play_test_db',id:'id',max:100});	//初始化存诸数据类,name:名称,必须惟一， id:关键字，标识惟一对象,默认‘id’ max:最大存储,默认100
	favDB = new DB({name:'recent_play_test_db',id:'id',max:100});	//初始化存诸数据类,name:名称,必须惟一， id:关键字，标识惟一对象,默认‘id’ max:最大存储,默认100
	
	mBox = new LatticeBox({					
		id:'indexContent',
		moveLeft:function(){
			level2Menu.setFocus();
		},
		onclick:function(data,tar){
			$.log($.toJSON(data));
		},
	});
	
	level2Menu = new Level2Menu({				//初始化左边导航
		id:'navBox',
		moveRight:function(){			//向右事件
			var type = this.getCurType();
			if(type.id == 'index'){  //如果是首页
				mBox.setFocus();
			}else{
				iconList.setFocus();
			}
		},
		clickType:function(type,id){		//一级菜单，没有二级是触发的函数
			$('#nav').html(type.name);
			if(type.id == 'index'){				//如果是首页
				$('#listBox').hide();
				$('#indexBox').show();
				if(!type.list){			//如果首页数据还未加载
					type.list = loadIndexList();		//加载首页数据
					$.log($.toJSON(type.list));
					mBox.setDataList(type.list);
				}
			}else{
				$('#listBox').show();
				$('#indexBox').hide();
			}
		},
		clickItem:function(data,type,id){	//点击二级菜单,data,二级菜单数据,type:一级菜单数据
			$('#nav').html(type.name+'&nbsp;>&nbsp;'+data.name);
			$('#listBox').show();
			$('#indexBox').hide();
			if(data.list){
				textList.setDataList(data.dataList);
			}else{
				loadDataList(data,afterLoadDataList);
			}
		},
	});
	
	
	iconList = new IconList({			//初始化右边导航
		id:'iconBox',
		moveLeft:function(){
			level2Menu.setFocus();
		},
		formatHtml:function(i,row,data){
			var img = $.getImageUrl(data.image,DEFAULT_IMG); 
			var con = '<div class="icon">';
				con += '<img src="'+img+'" />';
				con += '<a href="#" id="'+this.target+'-a-'+i+'" tar="'+i+'" row="'+row+'">&nbsp;</a>';
			con += '</div>';
			return con;
		},
		onfocus:function(data,id){
			var img = $.getImageUrl(data.image,DEFAULT_IMG); 
			var con = '';
			con += '<div class="title">'+data.name+'</div>';
			con += ' <div class="split">&nbsp;</div>';
			con += '<div class="logo"><img src="'+img+'"/></div>';
			con += '<div class="ul">';
				con += '<div class="li">主演:abckdeft</div>';
				con += '<div class="li">主演:abckdeft</div>';
			con += '</div>';
			$('#infoBox').html(con);
		},
		onclick:function(data,id){
			$.log($.toJSON(data));
		}
	}); 
	
	 initData(afterInitData);
	
});

function afterInitData(list){
	if(!list){list = new Array();}
	
	
	var favType = {
		id:'fav',
		name:'收藏夹',
		list:favDB.getList(),
		totalPage:1,
		curPage:1,
	}
	list.unshift(favType);
	
	var indexType = {			//首页对应的分类
		id:'index',
		name:'首页',
	}
	list.unshift(indexType);
	
	typeList = list;
	level2Menu.setDataList(typeList,true);
	level2Menu.Click(1);
}

function afterLoadDataList(data,list){
	if(!list){list = new Array();}
	
	
	
	data.dataList = list;
	iconList.setDataList(data.dataList);
}



